package cn.yang.domain.award.service;

import cn.yang.domain.award.event.SendAwardMessageEvent;
import cn.yang.domain.award.model.aggregate.UserAwardRecordAggregate;
import cn.yang.domain.award.model.entity.DistributeAwardEntity;
import cn.yang.domain.award.model.entity.TaskEntity;
import cn.yang.domain.award.model.entity.UserAwardRecordEntity;
import cn.yang.domain.award.model.valobj.TaskStateVO;
import cn.yang.domain.award.repository.IAwardRepository;
import cn.yang.domain.award.service.distribute.IDistributeAward;
import cn.yang.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0
 * @ClassName: cn.yang.domain.award.service
 * @Description: TODO
 * @Author: YJ
 */
@Service
@Slf4j
public class AwardService implements IAwardService {

    private final IAwardRepository awardRepository;

    private final SendAwardMessageEvent sendAwardMessageEvent;

    private final Map<String, IDistributeAward> distributeAwardMap;

    public AwardService(Map<String, IDistributeAward> distributeAwardMap, SendAwardMessageEvent sendAwardMessageEvent, IAwardRepository awardRepository) {
        this.distributeAwardMap = distributeAwardMap;
        this.sendAwardMessageEvent = sendAwardMessageEvent;
        this.awardRepository = awardRepository;
    }

    @Override
    public void saveUserAwardRecord(UserAwardRecordEntity userAwardRecordEntity) {
        // 构建消息对象
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage =
                SendAwardMessageEvent.SendAwardMessage.builder()
                        .userId(userAwardRecordEntity.getUserId())
                        .orderId(userAwardRecordEntity.getOrderId())
                        .awardId(userAwardRecordEntity.getAwardId())
                        .awardTitle(userAwardRecordEntity.getAwardTitle())
                        .awardConfig(userAwardRecordEntity.getAwardConfig())
                        .build();

        BaseEvent.EventMessage<SendAwardMessageEvent.SendAwardMessage> sendAwardMessageEventMessage =
                sendAwardMessageEvent.buildEventMessage(sendAwardMessage);

        //构建任务对象
        TaskEntity taskEntity = TaskEntity.builder()
                .userId(userAwardRecordEntity.getUserId())
                .topic(sendAwardMessageEvent.topic())
                .messageId(sendAwardMessageEventMessage.getId())
                .message(sendAwardMessageEventMessage)
                .taskState(TaskStateVO.create)
                .build();

        // 构建聚合对象
        UserAwardRecordAggregate userAwardRecordAggregate = UserAwardRecordAggregate.builder()
                .userAwardRecord(userAwardRecordEntity)
                .taskEntity(taskEntity)
                .build();
        awardRepository.saveUserAwardRecord(userAwardRecordAggregate);
    }

    @Override
    public void distributeAward(DistributeAwardEntity distributeAwardEntity) {
        // 奖品KEy
        String awardKey = awardRepository.queryAwardKey(distributeAwardEntity.getAwardId());
        if (null == awardKey) {
            log.error("分发奖品，奖品ID不存在。awardKey:{}", awardKey);
            return;
        }
        IDistributeAward distributeAward = distributeAwardMap.get(awardKey);
        if (null == distributeAward) {
            log.error("分发奖品，对应的服务不存在。awardKey:{}", awardKey);
            //todo:后续完善全部奖品后开启异常
//            throw new RuntimeException("分发奖品，奖品" + awardKey + "对应的服务不存在");
            return;
        }
        // 发奖
        distributeAward.giveOutPrices(distributeAwardEntity);
    }
}
